﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using hxy.CopyRight;

namespace TestProject
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class MainUnitTest
	{
		public MainUnitTest() { }

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[ClassInitialize()]
		public static void ClassInitialize(TestContext testContext)
		{
			ProductRegister.RegisterInfoProvider = new FileRegisterInfoProvider(@"D:\1.txt");
		}

		[TestMethod]
		public void TestRegisterCode()
		{
			var ran = new Random();

			DateTime useTimeLimit = new DateTime(ran.Next(2000, 2100), ran.Next(1, 12), ran.Next(1, 28));
			DateTime registerTime = new DateTime(ran.Next(2000, 2100), ran.Next(1, 12), ran.Next(1, 28));

			string code = RegisterCodeManager.EncryptCode(useTimeLimit, registerTime);

			Assert.IsNotNull(code);
			Assert.IsTrue(code.Length > 0 && code.Length < 100);

			var codeInfo = RegisterCodeManager.DecryptCode(code);

			Assert.IsTrue(codeInfo.RegisterTimeLimit == registerTime);
			Assert.IsTrue(codeInfo.UseTimeLimit == useTimeLimit);
		}

		[TestMethod]
		public void TestServer()
		{
			var USE_TIME = new DateTime(2012, 12, 30);

			try
			{
				DateTime useTime;
				if (!ProductRegister.HasRegistered(out useTime))
				{
					var code = RegisterCodeManager.EncryptCode(
						USE_TIME,
						DateTime.Now.AddDays(1d)
						);

					ProductRegister.Register(code);
					Assert.IsTrue(ProductRegister.HasRegistered(out useTime));
				}

				Assert.AreEqual(USE_TIME, useTime);
			}
			finally
			{
				ProductRegister.Clear();
			}
		}

		//[TestMethod]
		//public void TestDES()
		//{
		//    string pwd = "hxy";

		//    string plainText = new Random().Next().ToString();
		//    var ciperText = DES.Encrypt(plainText, pwd);
		//    var plainText2 = DES.Decrypt(ciperText, pwd);

		//    Assert.AreEqual(plainText, plainText2);
		//}
	}
}